package aden.SifWar;

import aden.Generic.Gen2DView;
import aden.Generic.GenAction;
import aden.Generic.GenActionHier;
import aden.Generic.GenActionUp;
import aden.Generic.GenIconMenu;
import aden.Generic.GenMain;
import aden.Generic.GenViewPointsTool;
import aden.Generic.GenViewTool;
//import aden.Generic.GenPopup;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.SurfaceHolder;


class SifWarViewToolPan extends GenViewPointsTool
{
	public SifWarViewToolPan(Gen2DView inView)
	{
		super(inView,1);
	}

	
	//to be overloaded
	public boolean ProcessEnd(boolean inIsValid)
	{
		mView.Pan(mXs[0],mYs[0]);
		return true;
	}

}

class SifWarActionPan extends GenAction
{
    private SifWarViewToolPan mTool;
    
    private SifWarView mView;
    SifWarActionPan(SifWarView inView,
                             SifWarMenu inMenu,
                             Bitmap       inIcon)
    {
        super(inMenu,inIcon);
        mView = inView;
        mTool = new SifWarViewToolPan(inView);
    }
    
    public boolean Process()
    {
    	mView.PushTool(mTool);
        return true;
    }
    
}



class SifWarViewToolSelect extends GenViewPointsTool
{
	private SifWarMenu mMenu;
	public SifWarViewToolSelect(Gen2DView inView,
            SifWarMenu inMenu)
	{
		super(inView,1);
		mMenu = inMenu;
	}
	
	//to be overloaded
	public boolean ProcessEnd(boolean inIsValid)
	{
		((SifWarView)mView).Select(mXs[0],mYs[0]);
		SifWarUnit theSelection = ((SifWarView)mView).GetSelection();
		if (theSelection != null)
		{
			mMenu.SetHierAction(theSelection.GetActions(mMenu.GetHierAction()));
		}
		((SifWarView)mView).RefreshAdd();
		((SifWarView)mView).FlushRefresh();
		return true;
	}
}


class SifWarActionSelect extends GenAction
{
    private SifWarViewToolSelect mTool;
    
    private SifWarView mView;
    SifWarActionSelect(SifWarView inView,
                             SifWarMenu inMenu,
                             Bitmap       inIcon)
    {
        super(inMenu,inIcon);
        mView = inView;
        mTool = new SifWarViewToolSelect(inView,inMenu);
    }
    
    public boolean Process()
    {
    	mView.PushTool(mTool);
        return true;
    }
    
}


class SifWarViewToolHelp extends GenViewPointsTool
{
	private SifWarMenu mMenu;
	public SifWarViewToolHelp(Gen2DView inView,
            SifWarMenu inMenu)
	{
		super(inView,1);
		mMenu = inMenu;
	}
	
	//to be overloaded
	public boolean ProcessEnd(boolean inIsValid)
	{
//		((SifWarView)mView).Help(mXs[0],mYs[0]);
//		SifWarUnit theHelpion = ((SifWarView)mView).GetHelpion();
//		if (theHelpion != null)
//		{
//			mMenu.SetHierAction(theHelpion.GetActions(mMenu.GetHierAction()));
//		}
//		((SifWarView)mView).RefreshAdd();
//		((SifWarView)mView).FlushRefresh();
		return true;
	}
}


class SifWarActionHelp extends GenAction
{
    private SifWarViewToolHelp mTool;
    
    private SifWarView mView;
    SifWarActionHelp(SifWarView inView,
                             SifWarMenu inMenu,
                             Bitmap       inIcon)
    {
        super(inMenu,inIcon);
        mView = inView;
        mTool = new SifWarViewToolHelp(inView,inMenu);
    }
    
    public boolean Process()
    {
    	mView.PushTool(mTool);
        return true;
    }
}


class SifWarActionChangeViewOrderOption extends GenAction
{
	private Bitmap[] mViewOrderOptionIcons;
	private int mViewOrderOption;

	private SifWarView mView;
	SifWarActionChangeViewOrderOption(SifWarView inView,
			SifWarMenu inMenu,
			Bitmap[]       inIcons)
			{
		super(inMenu,null);
		mView = inView;

		mViewOrderOptionIcons = inIcons;

		mViewOrderOption = 0;
		mIcon = mViewOrderOptionIcons[mViewOrderOption];
	}

	public boolean Process()
	{
		++mViewOrderOption;
	    if (mViewOrderOption == SifWarView.ViewOrderOption_Undefined)
	    {
	    	mViewOrderOption = 0;
	    }
	    SetViewOrderOption(mViewOrderOption);
	    SifWarUnit theUnit = mView.GetSelection();
		((SifWarView)mView).RefreshAdd();
		((SifWarView)mView).FlushRefresh();
		return true;
	}
	
	public boolean SetViewOrderOption(int inViewOrderOption)
	{
		mIcon = mViewOrderOptionIcons[mViewOrderOption];
		mView.ChangeViewOrderOption(mViewOrderOption);
		mMenu.Refresh();
		return true;
	}
}


class SifWarActionChangeViewDateOption extends GenAction
{
	private Bitmap[] mViewDateOptionIcons;
	private int mViewDateOption;

	private SifWarView mView;
	SifWarActionChangeViewDateOption(SifWarView inView,
			SifWarMenu inMenu,
			Bitmap[]       inIcons)
			{
		super(inMenu,null);
		mView = inView;

		mViewDateOptionIcons = inIcons;

		mViewDateOption = 0;
		mIcon = mViewDateOptionIcons[mViewDateOption];
	}

	public boolean Process()
	{
		++mViewDateOption;
	    if (mViewDateOption == SifWarView.ViewDateOption_Undefined)
	    {
	    	mViewDateOption = 0;
	    }
	    SetViewDateOption(mViewDateOption);
	    SifWarUnit theUnit = mView.GetSelection();
		((SifWarView)mView).RefreshAdd();
		((SifWarView)mView).FlushRefresh();
		return true;
	}
	
	public boolean SetViewDateOption(int inViewDateOption)
	{
		mIcon = mViewDateOptionIcons[mViewDateOption];
		mView.ChangeViewDateOption(mViewDateOption);
		mMenu.Refresh();
		return true;
	}
}



class SifWarActionTurn extends GenAction
{
    private SifWarView mView;
    SifWarActionTurn(SifWarView inView,
                             SifWarMenu inMenu,
                             Bitmap       inIcon)
    {
        super(inMenu,inIcon);
        mView = inView;
    }
    
    public boolean Process()
    {
    	((SifWarMain)mView.GetMain()).GetSifWarWorld().Evolve();
	((SifWarView)mView).RefreshEnv();
	((SifWarView)mView).FlushRefresh();

	// essai d'ajout de pop up
// 	theSurface = new GenPopup(((SifWarMain)mView.GetMain()),
// 				  mView);

        return true;
    }
    
}

class SifWarActionChangePlayer extends GenAction
{
    private SifWarView mView;
    SifWarActionChangePlayer(SifWarView inView,
                             SifWarMenu inMenu,
                             Bitmap       inIcon)
    {
        super(inMenu,inIcon);
        mView = inView;
    }
    
    public boolean Process()
    {
    	((SifWarMain)mView.GetMain()).GetPlayerMgr().ChangePlayer();
	((SifWarView)mView).RefreshEnv();
	((SifWarView)mView).FlushRefresh();
        return true;
    }
    
}

class SifWarActionGenerateWorld extends GenAction
{
    private SifWarView mView;
    SifWarActionGenerateWorld(SifWarView inView,
                             SifWarMenu inMenu,
                             Bitmap       inIcon)
    {
        super(inMenu,inIcon);
        mView = inView;
    }
    
    public boolean Process()
    {
    	mMenu.SetHierAction(mView.GetSifWarWorld().GetGenerationAction(mMenu.GetHierAction()));
        return true;
    }
    
}


class SifWarViewToolOrder extends GenViewPointsTool
{
	public SifWarUnit mUnit;
	public SifWarViewToolOrder(Gen2DView inView)
	{
		super(inView,1);
		mUnit = null;
	}

	public void SetUnit(SifWarUnit inUnit)
	{
		mUnit = inUnit;
	}
	
	//to be overloaded
	public boolean ProcessEnd(boolean inIsValid)
	{
		if (!inIsValid || mUnit == null)
		{
			return false;
		}
		return ProcessOrder();
	}
	public boolean ProcessOrder()
	{
		return true;
	}
}

public class SifWarMenu extends GenIconMenu {
    
    public SifWarMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SifWarMain GetSifWarMain() {return (SifWarMain)mMain;}
    
    public void Init(GenMain inMain)
    {
        super.Init(inMain);
        Resources res = mContext.getResources();
        
        // on initialise le contenu du menu
        
        GenAction[] theContent = new GenAction[6];
        theContent[0] = new SifWarActionPan(GetSifWarMain().GetSifWarView(),
        		this,
        		BitmapFactory.decodeResource(res,
        				R.drawable.icon_pan));
        theContent[1] = new SifWarActionSelect(GetSifWarMain().GetSifWarView(),
        		this,
        		BitmapFactory.decodeResource(res,
        				R.drawable.icon_select));
        
        Bitmap[] theBitmaps = new Bitmap[2];
        theBitmaps[0] = BitmapFactory.decodeResource(res,
				R.drawable.icon_hill);
        theBitmaps[1] = BitmapFactory.decodeResource(res,
				R.drawable.icon_forest);
        theContent[2] = new SifWarActionChangeViewOrderOption(GetSifWarMain().GetSifWarView(),
        		this,
        		theBitmaps);
        
        theContent[3] = new SifWarActionTurn(GetSifWarMain().GetSifWarView(),
        		this,
        		BitmapFactory.decodeResource(res,
        				R.drawable.icon_turn));
        
        theContent[4] = new SifWarActionChangePlayer(GetSifWarMain().GetSifWarView(),
        		this,
        		BitmapFactory.decodeResource(res,
        				R.drawable.icon_chgplayer));
        
        theContent[5] = new SifWarActionGenerateWorld(GetSifWarMain().GetSifWarView(),
        		this,
        		BitmapFactory.decodeResource(res,
        				R.drawable.icon_worldgeneration));
        
        mHierAction = new GenActionHier(this, 
                                             BitmapFactory.decodeResource(res,
                                                                          R.drawable.icon_up),
                                             theContent); 
        
        mThread.Refresh();
    }
    
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    	mThread.setSurfaceSize(width, height);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // start the thread here so that we don't busy-wait in run()
        // waiting for the surface to be created
    	mThread.setRunning(true);
    	mThread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        mThread.setRunning(false);
        while (retry) {
            try {
            	mThread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }
    }
    public boolean SetHierAction(GenActionHier inHier)
    {
        mHierAction = inHier;
        mThread.Refresh();
        return true;
    }
    public boolean Refresh()
    {
    	mThread.Refresh();
        return true;
    }
}


